<?php
class ReceiveNormalIM {

	private $message;
	private $blacklist ;
	private $flag = true;
	function ReceiveNormalIM() {
		$this->blacklist = new Blacklist();
		if ($this->blacklist->isInBlacklist()) {
			$this->flag = false;
			return;
		}
		$_POST[id] = 0;
		$_POST[SendTime] = date("Y-m-d H:i:s", $_POST[SendTime] + 8 * 3600);
		$message = array (
			'id' => $_POST[id],
			'Copyright' => $_POST[Copyright],
			'Event' => $_POST[Event],
			'ClusterNum' => $_POST[ClusterNum],
			'ClusterName' => $_POST[ClusterName],
			'Sender' => $_POST[Sender],
			'Nick' => $_POST[Nick],
			'Message' => $_POST[Message],
			'SendTime' => $_POST[SendTime],
			'Version' => "",
			'RobotQQ' => $_POST[RobotQQ],
			'ApiPort' => $_POST[ApiPort]
		);

		@ DB :: SaveTableRow('td_normal_mess', $message);
		if($_POST[Sender]!="261589289") { 
			$api = new Api("SendMessage");
			//$api->doApi("261589289",$_POST[Nick]."({$_POST[Sender]}):\n".$_POST[Message]);
		}
		if(!isset($_SESSION['is_friend'])) { 
			$_SESSION['is_friend'] =1 ;
			Favorable::updateFriendFlag();
		}
	}

	public function run() {
		if (!$this->flag)
			return;
				
		if (!isset ($_SESSION['NOWMODE'])) {
			if (@ preg_match('/^\/(\S+)\s*(.*)$/is', trim($_POST[Message]), $m)) {
				$class = $this->getFunByKey($m[1]);
				if($class!="") {
					$_SESSION['NOWMODE']=$class;
					$modelfile = ROOT_PATH . "Lib/Mode/{$class}.class.php";
					require_once ("{$modelfile}");
					$do = new $class;
					$do->run();
					$this->message = $do->message;
				}
			}else {
				$modelfile = ROOT_PATH . "Lib/Mode/Teach.class.php";
				require_once ("{$modelfile}");
				$do = new Teach();
				$do->getTeachWord();
				$this->message = $do->message;
				
			}
		} else {
			$class = $_SESSION['NOWMODE'];
			$modelfile = ROOT_PATH . "Lib/Mode/{$class}.class.php";
			require_once ("{$modelfile}");
			$do = new $class;
			$do->run();
			$this->message = $do->message;
		}
	}

	private function getFunByKey($key) {
		$ret2 = @ DB :: LimitQuery('td_mode_function', array (
			'condition' => array (
				'key' => $key
			)
		), false);
		if ($ret2)
			return $ret2[0][funname];
		else
			return "";
	}

	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}

}
?>